'***************
'    利用字符串修改数组节点
'    目标值rank可为负数
'***************

Function FixElement(arr, eStr, Optional rank = 1, Optional ByVal delimiter = ",")
    Dim Str$, arr1
    Dim element%
    Str = Join(arr, delimiter) & delimiter
    element = CountStr(Str, delimiter)
    If rank > 0 Then
        rank = rank
    ElseIf rank < 0 Then
        rank = element + 1 + rank
    End If

    If rank > 0 And rank <= element Then
        FixElement = Left(Str, GetElementPosition(Str, delimiter, rank - 1)) & eStr & delimiter & Right(Str, Len(Str) - GetElementPosition(Str, delimiter, rank))
        FixElement = Left(FixElement, Len(FixElement) - 1)
    End If
End Function


'***************
'    利用字符串为数组切片
'    目标值rank可为负数
'***************

Function sliceElement(arr, Optional sRank = 1, Optional eRank = 1, Optional ByVal delimiter = ",")
    Dim Str$, arr1
    Dim element%
    Str = delimiter & Join(arr, delimiter) & delimiter
    element = CountStr(Str, delimiter)
    If sRank > 0 Then
        sRank = sRank
    ElseIf sRank < 0 Then
        sRank = element + 1 + sRank
    End If
    If eRank > 0 Then
        eRank = eRank + 1
    ElseIf eRank < 0 Then
        eRank = element + 1 + eRank
    End If
    If rank >= 0 And rank <= element Then
        sliceElement = Mid(Str, GetElementPosition(Str, delimiter, sRank), (Len(Str) - GetElementPosition(Str, delimiter, sRank)) - (Len(Str) - GetElementPosition(Str, delimiter, eRank)))
        sliceElement = Right(sliceElement, Len(sliceElement) - 1)
    End If
End Function


'***************
'    利用字符串增加数组节点
'    目标值rank可为负数
'***************

Function AddElement(arr, eStr, Optional rank = 1, Optional ByVal delimiter = ",")
    Dim Str$, arr1
    Dim element%
    Str = Join(arr, delimiter) & delimiter
    element = CountStr(Str, delimiter)
    If rank > 0 Then
        rank = rank
    ElseIf rank < 0 Then
        rank = element + 1 + rank
    End If
    If rank >= 0 And rank <= element Then
        AddElement = Left(Str, GetElementPosition(Str, delimiter, rank)) & eStr & delimiter & Right(Str, Len(Str) - GetElementPosition(Str, delimiter, rank))
        AddElement = Left(AddElement, Len(AddElement) - 1)
    End If
End Function


'***************
'    利用字符串删除数组节点
'    目标值rank可为负数
'***************

Function DelElement(arr, Optional rank = 1, Optional ByVal delimiter = ",")
    Dim Str$, arr1
    Dim element%
    Str = Join(arr, delimiter) & delimiter
    element = CountStr(Str, delimiter)
    If rank > 0 Then
        rank = rank
    ElseIf rank < 0 Then
        rank = element + 1 + rank
    End If
    
    If rank > 0 And rank <= element Then
        DelElement = Left(Str, GetElementPosition(Str, delimiter, rank - 1)) & Right(Str, Len(Str) - GetElementPosition(Str, delimiter, rank))
        DelElement = Left(DelElement, Len(DelElement) - 1)
    End If
End Function


'***************
'    依赖:利用split函数转换字符串为数组
'***************

Function StrToArray(ByVal Str As String, Optional ByVal delimiter = ",")
    Dim arr
    arr = Split(Str, delimiter)
    StrToArray = arr
End Function

Function StrToNumArray(ByVal Str As String, Optional ByVal delimiter = ",")
    Dim arr() As Integer
    arr = Split(Str, delimiter)
    StrToNumArray = arr
End Function

'***************
'    依赖:计算节点个数
'***************

Private Function CountStr(ByVal strInput As String, ByVal strFind As String) As Integer
    CountStr = Len(strInput) - Len(Replace(strInput, strFind, ""))
End Function


'***************
'    依赖:获取节点坐标
'***************
Private Function GetElementPosition(Str As String, ByVal element As String, Optional ByVal rank As Integer = 0)
    Dim Position, WhichPosition%, Counter%
    WhichPosition = InStr(Str, element)
    
    Position = WhichPosition
    Counter = 1
    Do
        If rank = Counter Then
            GetElementPosition = Int(WhichPosition)
            Exit Function
        End If
        
        WhichPosition = InStr(WhichPosition + 1, Str, element)
        
        If WhichPosition = 0 Then
            Position = 0
            Exit Do
        End If
        
        Counter = Counter + 1
        'Position = Position & "," & WhichPosition
    Loop
    GetElementPosition = Position
End Function

